package com.situ.web.dao.impl;

import com.situ.web.dao.IUserDao;
import com.situ.web.pojo.User;
import com.situ.web.pojo.query.UserQuery;
import com.situ.web.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao {
    @Override
    public List<User> selectByPage(UserQuery userQuery) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<User> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            // from users where name like ? and age=?
            // from users where  age=?
            // from users where  name like ?
            // from users
            // 有这个搜索条件，就拼接上 and ...
            // from users where 1=1 and name like ? and age=?
            // from users where 1=1 and age=?
            //String sql = "select id,name,password,age,address from users where and name like ? and age=? limit ?,?";
            String sql = "select id,name,password,age,address from users where 1=1 ";

            List<Object> queryList = new ArrayList<>();
            String queryName = userQuery.getName();
            if (queryName != null && !queryName.equals("")) {
                sql += "and name like ? ";
                queryList.add("%" + queryName + "%");
            }
            String queryAge = userQuery.getAge();
            if (queryAge != null && !queryAge.equals("")) {
                sql += "and age=? ";
                queryList.add(Integer.parseInt(queryAge));
            }

            sql += "limit ?,?";
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                statement.setObject(i + 1, queryList.get(i));
            }

            int offset = (userQuery.getPage() - 1) * userQuery.getLimit();
            statement.setInt(queryList.size() + 1, offset);
            statement.setInt(queryList.size() + 2, userQuery.getLimit());
            System.out.println(statement);

            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                int age = resultSet.getInt("age");
                String address = resultSet.getString("address");

               User user = new User(id, name, password, age, address);
               list.add(user);
            }
            for (User user : list) {
                System.out.println(user);
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }

        return list;
    }

    @Override
    public int selectTotalCount(UserQuery userQuery) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();

            String sql = "select count(*) from users where 1=1 ";

            List<Object> queryList = new ArrayList<>();
            String queryName = userQuery.getName();
            if (queryName != null && !queryName.equals("")) {
                sql += "and name like ? ";
                queryList.add("%" + queryName + "%");
            }
            String queryAge = userQuery.getAge();
            if (queryAge != null && !queryAge.equals("")) {
                sql += "and age=? ";
                queryList.add(Integer.parseInt(queryAge));
            }

            statement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                statement.setObject(i + 1, queryList.get(i));
            }

            System.out.println(statement);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                count = resultSet.getInt(1);
            }

        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }

        return count;
    }

    @Override
    public User login(String name, String password) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,password,age,address from users where name=? and password=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, name);
            statement.setString(2, password);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                int id = resultSet.getInt("id");
                int age = resultSet.getInt("age");
                String address = resultSet.getString("address");

                user = new User(id, name, password, age, address);
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }

        return user;
    }

    @Override
    public int deleteById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from users where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            System.out.println(statement);
            count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }

        return count;
    }

    @Override
    public int add(User user) {
        Connection connection = null;
        PreparedStatement statement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into users(name,password,age,address) values(?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getName());
            statement.setString(2, user.getPassword());
            statement.setInt(3, user.getAge());
            statement.setString(4, user.getAddress());
            System.out.println(statement);
            count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }

        return count;
    }

    @Override
    public User selectById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,password,age,address from users where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                int age = resultSet.getInt("age");
                String address = resultSet.getString("address");

                user = new User(id, name, password, age, address);
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }

        return user;
    }

    @Override
    public int update(User user) {
        Connection connection = null;
        PreparedStatement statement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update users set name=?,password=?,age=?,address=? where id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getName());
            statement.setString(2, user.getPassword());
            statement.setInt(3, user.getAge());
            statement.setString(4, user.getAddress());
            statement.setInt(5, user.getId());
            System.out.println(statement);
            count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }

        return count;
    }
}
